<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Community as CommunityModel;

/**
 * 企业社群
 * Class CommunityService
 * @package app\admin\service
 */
class CommunityService
{
    /**
     * 企业社群列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCommunityList($where, $page, $pagesize)
    {
        $data = CommunityModel::getCommunityList($where, $page, $pagesize);

        foreach ($data as $k => $v) {
            $data[$k]['headimg'] = '<div><img class="headimgs" title="点我查看原图" onclick="lookimg(this.src)" src="' . $v['headimg'] . '" \' + \'alt=""></div><p>' . emojiDecode($v['nickname']) . '</p>';

            $data[$k]['remarks'] = '-';
            // 已驳回，获取最新原因
            if ($v['status'] == -1) {
                $data[$k]['remarks'] = CommunityRejectService::findCommunityRejectValue(['resid' => $v['id']]);
            }

            // 地区
            $city = $v['ecity'] ?: $v['city'];
            $data[$k]['city_name'] = LocationService::getLocationValue(['location_id' => $city]);

            // 行业-企业
            $ename = $v['ename'] ?: '-';
            $data[$k]['cname'] = '<p class="color3">' . $v['cate_name'] . '</p><p>' . $ename . '</p>';

            // 二维码
            $data[$k]['qrcode'] = '<div><img class="otherimages" title="点我查看原图" onclick="lookimg(this.src)" src="' . config('url.url') . $v['qrcode'] . '" \' + \'alt=""></div>';

            // logo
            $data[$k]['logo'] = '<div><img class="otherimages" title="点我查看原图" onclick="lookimg(this.src)" src="' . config('url.url') . $v['logo'] . '" \' + \'alt=""></div>';
        }

        return $data;
    }

    /**
     * 企业社群统计
     * @param $where
     * @return int
     */
    public static function countCommunityList($where)
    {
        return CommunityModel::countCommunityList($where);
    }

    /**
     * 指定企业社群
     * @param $where
     * @param int $assemble 0不组装  1组装
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findCommunity($where, $assemble = 0)
    {
        $info = CommunityModel::findCommunity($where);
        if (!$assemble) {
            return $info;
        }

        $info['qrcode'] = $info['qrcode'] ? config('url.url') . $info['qrcode'] : '';
        $info['logo'] = $info['logo'] ? config('url.url') . $info['logo'] : '';
        // 企业数据
        $info['enterprise'] = EnterpriseService::findEnterpriseValue(['id' => $info['eid']]);
        // 社群行业
        $info['resourceCate'] = IndustryCateService::findIndustryCateValue(['id' => $info['cid']]);
        // 是否代管
        $info['is_escrow'] = $info['is_escrow'] ? '是' : '否';

        return $info;
    }

    /**
     * 更新企业社群
     * @param $data
     * @param $where
     * @return CommunityModel
     */
    public static function updateCommunity($data, $where)
    {
        return CommunityModel::updateCommunity($data, $where);
    }

    /**
     * 创建企业社群
     * @param $data
     * @return CommunityModel|\think\Model
     */
    public static function createCommunity($data)
    {
        return CommunityModel::createCommunity($data);
    }
}